from moodlefuse.files import Directory, RegularFile
import os

class Discussion(RegularFile):

    def read(self, size, offset):
        if not os.path.exists(self.filepath):
            content = self.api.get_discussion(self.url)
            self.cache_content(content)
            
            self.attrs['st_size'] = len(content)

        with open(self.filepath, 'rb+') as file:
            file.seek(offset, 0)
            return file.read(size)

    def cache_content(self, content):
        with open(self.filepath, 'w+') as file:
            file.write(content)

class Forum(Directory):

    def load_files(self):
        self.files = self.api.get_discussions(self.url)

class Forums(Directory):

    def load_files(self):
        self.files = self.api.get_forums(self.url)
  